#include "MainHeader.h"

int LoadEnemies(){
//First we dim our need varibles
RHeader* RoomHeader=GameEngine->Members.RoomHeader;
u8 loopcounter=0;
u8 max=0;
int i=0,j=0;
int EnemySet=0;
long curroffset=0;
u32 offsetlist[3]={RoomHeader->lSpritePointer,RoomHeader->lSpritePointer2,RoomHeader->lSpritePointer3};
nEnemyList* list[3]={&Enemy1,&Enemy2,&Enemy3};

loopcounter = 0;
//Code for set 1
 

	Enemy1.max = 0;
	Enemy2.max = 0;
	Enemy3.max = 0;
	for(i =0; i< 0x20;i++){
	 Enemy1.Enemies[i].X=Enemy1.Enemies[i].Y=0;
	 Enemy1.Enemies[i].Creature=0x10;
	 
	 Enemy2.Enemies[i].X=Enemy2.Enemies[i].Y=0;
	 Enemy2.Enemies[i].Creature=0x10;
	 
	 Enemy3.Enemies[i].X=Enemy3.Enemies[i].Y=0;
	 Enemy3.Enemies[i].Creature=0x10;
	}
	FILE* fp=fopen(GBA.FileLoc,"r+");
	if(fp){
		for(i =0;i<3;i++){
			fseek(fp,offsetlist[i]-0x8000000,SEEK_SET);
		
			max=0;
			for(j=0;j<0x20;j++) {
			   
				list[i]->Enemies[j].Y=fgetc(fp);
				list[i]->Enemies[j].X=fgetc(fp);
				list[i]->Enemies[j].Creature=fgetc(fp);
				if((list[i]->Enemies[j].Creature == 0xFF)) {
					break;
				}
			   (list[i]->max++);
		   
			}       
		}
	fclose(fp);
	}
    


   

			return 0;
}




int ShowSprites(u8 Number){
	int i = 0;
	int relX=0;
	int relY=0;
	int tSprite=0;
    int d=0;
	RECT myrect;
	HBRUSH curbrush;
	//RECT blah = {0,0,0,0};
		nEnemyList Sprites; 
			switch(Number){
    			case 0:
					Sprites=Enemy1;
					  break;
				case 1:
					Sprites=Enemy2;
					  break;
				case 2:
					Sprites=Enemy3;
					  break;
			}

   
		for(i = 0; i < Sprites.max;i++){
		 
        //Draw a rect around the sprite for distiction
        
		//	if(SpriteOAM[sprite_in[(Sprites.Enemies[i].Creature & 0xF)-1].sprtype]==0) continue;
        tSprite=((Sprites.Enemies[i].Creature) & 0xF)-1;
		//Draw a rectangle
		if(SpriteOAM[sprite_in[tSprite].sprtype]){
		  //If sprite doesn't exist draw nothing but the S
			if(SendMessage(GetDlgItem(Main,chkHS), BM_GETCHECK, 0, 0) ==0){
				curbrush = CreateSolidBrush(RGB(255, 0, 0));
				myrect.left = 	(Sprites.Enemies[i].X)*16-ZMSpritePos[sprite_in[tSprite].sprtype][0];
				myrect.top = (Sprites.Enemies[i].Y)*16 - ZMSpritePos[sprite_in[tSprite].sprtype][1]; 
				myrect.right = (OverallSize[tSprite].right -OverallSize[tSprite].left) + myrect.left; 
				myrect.bottom = myrect.top + (OverallSize[tSprite].bottom-OverallSize[tSprite].top);
				FrameRect(ThisBackBuffer.DC(), &myrect,curbrush);
				DeleteObject(curbrush);
			}
		//Draw the sprite
		SpritePics[tSprite].TransBlit(
		
			ThisBackBuffer.DC(),
			(Sprites.Enemies[i].X)*16-ZMSpritePos[sprite_in[tSprite].sprtype][0],
		
			(Sprites.Enemies[i].Y)*16 - ZMSpritePos[sprite_in[tSprite].sprtype][1],
	    	(OverallSize[tSprite].right -OverallSize[tSprite].left),	
	        (OverallSize[tSprite].bottom-OverallSize[tSprite].top),
			0,
			0);
		
	//Draw an S for sprite
		
		}
			if(SendMessage(GetDlgItem(Main,chkHS), BM_GETCHECK, 0, 0) == 0){
				myrect.left   =(Sprites.Enemies[i].X)*16;
				myrect.top    =(Sprites.Enemies[i].Y)*16;
				myrect.right  =myrect.left+16;
				myrect.bottom =myrect.top +16;
			    curbrush = CreateSolidBrush(RGB(255, 255, 255));
				FrameRect(ThisBackBuffer.DC(), &myrect,curbrush);
	            DeleteObject(curbrush);
			    TextOut(ThisBackBuffer.DC(), Sprites.Enemies[i].X*16 + 4,    
						     Sprites.Enemies[i].Y*16,"S",1);
			}
	  
		}

	
	
	return 0;
}



int SaveSprites(){
	int i =0;
RHeader* RoomHeader=GameEngine->Members.RoomHeader;
fseek(GBA.ROM,RoomHeader->lSpritePointer - 0x8000000,SEEK_SET);
if(Enemy1.max){
	for(i = 0;i<Enemy1.max;i++){
		fwrite(&Enemy1.Enemies[i].Y,1,1,GBA.ROM);
		fwrite(&Enemy1.Enemies[i].X,1,1,GBA.ROM);
		fwrite(&Enemy1.Enemies[i].Creature,1,1,GBA.ROM);
	}
	fputc(0xFF,GBA.ROM );
	fputc(0xFF,GBA.ROM );
	fputc(0xFF,GBA.ROM );

}
fseek(GBA.ROM,RoomHeader->lSpritePointer2 - 0x8000000,SEEK_SET);
if(Enemy2.max){
	for(i = 0;i<Enemy2.max;i++){
		fwrite(&Enemy2.Enemies[i].Y,1,1,GBA.ROM);
		fwrite(&Enemy2.Enemies[i].X,1,1,GBA.ROM);
		fwrite(&Enemy2.Enemies[i].Creature,1,1,GBA.ROM);
	}
fputc(0xFF,GBA.ROM );
fputc(0xFF,GBA.ROM );
fputc(0xFF,GBA.ROM );

}
fseek(GBA.ROM,RoomHeader->lSpritePointer3 - 0x8000000,SEEK_SET);
if(Enemy3.max){
	for(i = 0;i<Enemy3.max;i++){
		fwrite(&Enemy3.Enemies[i].Y,1,1,GBA.ROM);
		fwrite(&Enemy3.Enemies[i].X,1,1,GBA.ROM);
		fwrite(&Enemy3.Enemies[i].Creature,1,1,GBA.ROM);
	}
fputc(0xFF,GBA.ROM );
fputc(0xFF,GBA.ROM );
fputc(0xFF,GBA.ROM );


}
fclose(GBA.ROM);
GBA.ROM = fopen(GBA.FileLoc,"r+b");


	return 0;
}

int PopulateSpriteArray() {
	char buffer[512]={0};
    u32 tmp=0;
	u32 offsetc=0;; 
	u32 spriten=0;
	int i =0;
	FILE* fp=NULL;
	for(i=0;i<0xFF;i++) SpriteOAM[i]=0;
    char filepath[1024]={0};
	if((strlen(filepath)+20)>=1024){
		MessageBox(0,"Please move your file someplace else as the path is too long.","Error.",MB_OK);
		return 0;
	}
if(GameEngine->isMF()) sprintf(filepath,"%s\\SpriteList.txt", AppPath);
if (GameEngine->isZM()) sprintf(filepath,"%s\\MFSpriteList.txt", AppPath);
fp = fopen(filepath,"r");
	if(fp){
		while(!feof(fp)) {
			fgets(buffer, 256, fp);
			if(memcmp(buffer,"//",2) == 0)
				continue;
		
			if(memcmp(buffer,"END",3) == 0)
				break;
		
			sscanf(buffer, "%x", &tmp); 
			spriten = tmp;
	        sscanf(buffer+3, "%x", &offsetc);
			SpriteOAM[spriten] = offsetc;
		}

	fclose(fp);
	}
	return 0;
}


int InitPosArray(){
	int i = 0;
	for( i = 0; i < 256; i++){
		 ZMSpritePos[i][0] = 0;
		 ZMSpritePos[i][1] = 0;
	}
	//Poses are in tiles. 
     ZMSpritePos[0x24][1] = ZMSpritePos[0x23][1]=  ZMSpritePos[0x22][1] = 32;
    for(i = 0; i < 0xD;i++)
	ZMSpritePos[0x25+i][1] = (i%2 ? 32:64);
	ZMSpritePos[0x3A][0] = 16;
    ZMSpritePos[0x3A][1] = 96;
	ZMSpritePos[0x48][0] = 24;
    ZMSpritePos[0x4B][1] = 16;
	ZMSpritePos[0x4E][0] = 20;
    ZMSpritePos[0x95][0]=40;
	ZMSpritePos[0x95][1]=80;
	ZMSpritePos[0x50][0]=7;
	ZMSpritePos[0x50][1]=-16;
	ZMSpritePos[0x60][0]=16;
	ZMSpritePos[0x60][1]=72;
    ZMSpritePos[0x6E][0]=16;
	ZMSpritePos[0x8A][0]=48;
	ZMSpritePos[0x8A][1]=64;
	ZMSpritePos[0x8C][1]=32;
	ZMSpritePos[0x8C][0]=14;
	ZMSpritePos[0x8C][1]=32;
	ZMSpritePos[0xA3][0]=-16;
	ZMSpritePos[0xA3][1]=56;
	ZMSpritePos[0xA4][0]=90;
	ZMSpritePos[0xA4][1]=50;
	return 0;
}

int Preview(u8 Sprite){
//Lots of work goes on
	return 0;
}

int SavePal(PalData* palinfo,sprite_entry* spriteset, long *Palette) {
    u8 max=0;
	max=maxsprite;
	if(SSE==true)max=SpriteSet.total;
	long  addybuf;

 
	int  x;
	
	SpriteSet.usedPAL=0;
//	memset(&GBASprPal[128],0,sizeof(GBASprPal)-128);
//    memset(&SprPal,0,sizeof(SprPal));
   
	        GBA.EncodePal(GBASprPal, Palette, 16, 0); 
		  for( x = 0; x<max; x++) {
		if (palinfo[x].RomPointer  == 0)
			continue;
       
		fseek(GBA.ROM, palinfo[x].RomPointer , SEEK_SET);
		fread(&addybuf, 4, 1, GBA.ROM);
		fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
		fwrite(&GBASprPal[128+(palinfo[x].MemDst  )], 1, (palinfo[x].Size)*2, GBA.ROM);
         
//	if(paltransfer[x][1] == 0) continue;
			
	//	memcpy(&GBASprPal[128+(palinfo[x].MemDst  )  ], &transferpal, (palinfo[x].Size)*2);
           if(SSE) SpriteSet.usedPAL+=(palinfo[x].Size);
	}
    
	
   
  

	return 0;
}


int GetZMSetSZ(long* GFXSizes,long*PalSizes, sprite_entry* SpriteInfo){ 

long off =0;
int X=0;

//sprite_entry tempit[15]; 
u8  comp1[64192]={0};
u8  comp2[64192]={0};
u8 max=0;



compare=0xFF;


	for(X = 0; X<SpriteSet.total; X++) {

	

		if( SpriteInfo[X].sprdetail == compare)
			continue; //compare detail# to last detail#

		compare = SpriteInfo[X].sprdetail;                    //store last detail#

		if( SpriteInfo[X].sprdetail == 0x8)
			continue;      //if detail# = 8, dont load gfx & pals

		//gfx and palette loading
	
	
		fseek(GBA.ROM, sprgfxpnt + (SpriteInfo[X].sprtype - 0x10) * 4, SEEK_SET);
		fread(&off, 4, 1, GBA.ROM);
		fseek(GBA.ROM, off-0x8000000, SEEK_SET);
		fread(&PalSizes[X],4,1,GBA.ROM);
		PalSizes[X]= ((PalSizes[X] >>16)& 0xF8) * 2;
        fseek(GBA.ROM, off-0x8000000, SEEK_SET);
		fread(&comp1,1,32191,GBA.ROM);

		GFXSizes[X]=GBA.LZ77UnComp(comp1, comp2);
		//GFXSizes[X]+=SpriteInfo[X].sprdetail*0x800;
		GFXSizes[X]=	GFXSizes[X];
	}


	return 0;
}

int GetMFSetSZ(long* GFXSizes,long*PalSizes, sprite_entry* SpriteInfo){
	
long off =0;
int X=0;

//sprite_entry tempit[15]; 
u8 prevsprdetail=0;
u8 max=0;






	for(X = 0; X<SpriteSet.total; X++) {
	
	
		if((sprite_in[X].sprtype == 0) &&( X != 0)) break;
	
	
		if((sprite_in[X].sprdetail != prevsprdetail) && (sprite_in[X].sprdetail != 8))
		{

	
        GFXSizes[X]= MFSprSize[(SpriteInfo[X].sprtype-0x10)<<1]+(SpriteInfo[X].sprdetail * 0x800);

		PalSizes[X]=  (MFSprSize[(SpriteInfo[X].sprtype-0x10)<<1]/128)+(SpriteInfo[X].sprdetail * 16);
		}
		prevsprdetail= SpriteInfo[X].sprdetail;
	}

	


	return 0;
}